home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Idle / Periodic.cp < prev    next >
Text File  |  1997-06-28  |  1KB  |  80 lines

  1. // Periodic.cp
  2.  
  3. #ifndef Periodic_h
  4. #include "Periodic.h"
  5. #endif
  6.  
  7. Periodic::Periodic( const Method& method,
  8.                           uint32 thePeriod,
  9.                           bool startEnabled )
  10.   : Enableable( startEnabled ),
  11.      repeat( this, &Periodic::Repeat ),
  12.      inProgress( false ),
  13.      toRepeat( method ),
  14.      period( thePeriod ),
  15.      nextTime( 0 )
  16.   {
  17.     Assert( period < maxuint32 / 4 );
  18.     
  19.     if ( startEnabled )
  20.       {
  21.         nextTime = Tick::Now() + period;
  22.         repeat.DelayTo( nextTime );
  23.       }
  24.   }
  25.  
  26. void Periodic::BeEnabled()
  27.   {
  28.     nextTime = Tick::Now() + period;
  29.     
  30.     if ( !inProgress )
  31.         repeat.DelayTo( nextTime );
  32.   }
  33.  
  34. void Periodic::BeDisabled()
  35.   {
  36.     if ( !inProgress )
  37.         repeat.Cancel();
  38.   }
  39.  
  40. void Periodic::Repeat()
  41.   {
  42.     Assert( !inProgress );
  43.     Assert( Enabled() );
  44.     
  45.     nextTime += period;
  46.     
  47.     inProgress = true;
  48.     toRepeat();
  49.     inProgress = false;
  50.     
  51.     if ( Enabled() )
  52.       {
  53.         Tick now( Tick::Now() );
  54.         
  55.         if ( nextTime < now )
  56.             nextTime = now + period;
  57.         
  58.         repeat.DelayTo( nextTime );
  59.       }
  60.   }
  61.  
  62. void Periodic::Synchronize()
  63.   {
  64.     Assert( Enabled() );
  65.     nextTime = Tick::Now() + period;
  66.     
  67.     if ( !inProgress )
  68.       {
  69.         repeat.Cancel();
  70.         repeat.DelayTo( nextTime );
  71.       }
  72.   }
  73.  
  74. void Periodic::SetPeriod( uint32 p )
  75.   {
  76.     Assert( p < maxuint32 / 4 );
  77.     period = p;
  78.     Synchronize();
  79.   }
  80.